package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import base.DbConnect;
import model.Classinformation;

public class ClassinformationDao {
	 public static int add(Classinformation classinformation) {
			Connection conn = DbConnect.getConnect();
			PreparedStatement stmt = null;
			int res = -1;
			String sql = "insert into classinfomation values (?,?,?,?,?)";
			try {
				stmt = (PreparedStatement) conn.prepareStatement(sql);
				stmt.setString(1, classinformation.getCla_id());
				stmt.setString(2, classinformation.getM_id());
				stmt.setString(3, classinformation.getCla_name());
				stmt.setString(4, classinformation.getCla_grade());
				stmt.setString(5, classinformation.getCla_number());
				res = stmt.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return res;
		}

		public static ArrayList<Classinformation> select() {
			Connection conn = DbConnect.getConnect();
			Statement stmt = null;
			ResultSet rs = null;
			ArrayList<Classinformation> arrayList = new ArrayList<Classinformation>();
			
			String sql;
			sql = "SELECT * FROM classinfomation";
			try {
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					Classinformation course = new Classinformation();
					course.setCla_id(rs.getString("cla_id"));
					course.setM_id(rs.getString("m_id"));
					course.setCla_name(rs.getString("cla_name"));
					course.setCla_grade(rs.getString("cla_grade"));
					course.setCla_number(rs.getString("cla_number"));
					arrayList.add(course);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return arrayList;
		}
		
		public static Classinformation select(String cla_id) {
			Connection conn = DbConnect.getConnect();
			Statement stmt = null;
			ResultSet rs = null;
			Classinformation classinformation = new Classinformation();
			String sql;
			sql = "SELECT * FROM classinfomation where cla_id = '" + cla_id + "'";
			try {
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					classinformation.setCla_id(rs.getString("cla_id"));
					classinformation.setM_id(rs.getString("m_id"));
					classinformation.setCla_name(rs.getString("cla_name"));
					classinformation.setCla_grade(rs.getString("cla_grade"));
					classinformation.setCla_number(rs.getString("cla_number"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return classinformation;
		}
		
		public static int find(String cla_id) {
			Connection conn = DbConnect.getConnect();
			Statement stmt = null;
			ResultSet rs = null;
			String sql;
			sql = "SELECT * FROM classinfomation where cou_id = '" + cla_id + "'";
			try {
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					return 1;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return -1;
		}

		/**
		 * 删除信息
		 * @param department
		 * @return
		 */
		public static int delete(String cla_id) {
			Connection conn = DbConnect.getConnect();
			PreparedStatement stmt = null;
			int res = -1;
			String sql = "delete from classinfomation where cla_id = ?";
			try {
				stmt = (PreparedStatement) conn.prepareStatement(sql);
				stmt.setString(1, cla_id);
				res = stmt.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return res;
		}
		
		/**
		 * 更新信息
		 * @param department
		 * @return
		 */
		public static int updata(Classinformation classinformation) {
			Connection conn = DbConnect.getConnect();
			PreparedStatement stmt = null;
			int res = -1;
			try {
				String sql = "update classinfomation set m_id = ? ,cla_name = ? , cla_grade = ? ,cla_number = ? where cla_id = ?";
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, classinformation.getM_id());
				stmt.setString(2, classinformation.getCla_name());
				stmt.setString(3, classinformation.getCla_grade());
				stmt.setString(4, classinformation.getCla_number());
				stmt.setString(5, classinformation.getCla_id());
				res = stmt.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return res;
		}
}
